<?php
// +----------------------------------------------------------------------
// | VueAdmin框架[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2018 http://www.vueadmin.org
// +----------------------------------------------------------------------
// | VueAdmin承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: sept <917088650@qq.com>，开发者QQ群：63131383
// +----------------------------------------------------------------------

namespace app\system\controller;

use app\lib\Auth\Auth;
use app\lib\exception\ApiException;
use app\lib\exception\ForbiddenException;
use app\lib\exception\MissException;
use app\lib\requests\Response;
use app\lib\returnCode\Code;
use app\lib\returnCode\Msg;
use app\system\model\AdminUser as AdminModel;
use app\system\validate\AdminNew;
use app\system\validate\IDMustBePostiveInt;
use app\system\validate\UsernameAndPassword;

class Admin extends Base
{

    public function view() {
        //分页
        //TODO 参数默认化，并添加到配置参数里
        $limit = $this->request->get('limit', '20'); 
        $fields = $this->request->get('fields','*');
        $id = $this->request->get('id','');

        $admin_model = new AdminModel();

        if (is_numeric($id) && $id > 0){
            $data_info = $admin_model->field($fields)->where('id','=',$id)->select();
        } else {
            $data_info = $admin_model->field($fields)->paginate($limit);
        }


        if ($data_info->isEmpty()) {
            throw new ApiException(Code::NO_FIND_RELATE_RECORD,Msg::NO_FIND_RELATE_RECORD);
        }
        return Response::success($data_info);

    }

    public function create() {
        // 验证
        (new AdminNew())->goCheck();

        $data = $this->request->post();

        $data['last_login_ip'] = '';
        $admin_model = new AdminModel();

        $admin_model->add($data);

        return Response::success();
    }

    public function edit($id = '') {
        //其他管理员禁止修改超级管理员信息
        (new AdminNew())->goCheck();
        (new IDMustBePostiveInt())->goCheck();

        if ($id == 1 && Auth::user()->role_id != $id) {
            throw new ApiException(Code::NO_FIND_RELATE_RECORD,Msg::NO_FIND_RELATE_RECORD);
        }
        $data = $this->request->post();

        $admin_model = new AdminModel();

        $admin_model->edit($id, $data);

        return Response::success();
    }

    public function delete($id = 0) {

        (new IDMustBePostiveInt())->goCheck();

        $admin_model = new AdminModel();

        $admin_model->del($id);

        return Response::success();
    }

    public function login() {

        (new UsernameAndPassword())->goCheck();

        $data = $this->request->only('username,password,remember');

        $admin_model = new AdminModel();
        $token = $admin_model->login($data['username'], $data['password'],$data['remember']);

        if (false !== $token) {
            return Response::success($token);
        }
    }

    public function logout() {

        $admin_model = new AdminModel();
        $admin_model->logout();
        return Response::success();
    }

}